import axios from 'axios'

class Http {
  async request (method, url, { query, body, headers, responseType, withCredentials }) {
    try {
      return await axios.request({
        method,
        url,
        params: query,
        data: body,
        headers,
        timeout: 60*1000,
        withCredentials: !!withCredentials,
        responseType: responseType || 'json'
      })
    } catch ({ response }) {
      if (response) {
        throw { status: response.status, statusText: response, data: response.data }
      } else {
        throw { status: -1, statusText: 'offline', url, data: { msg: '请求超时或无网络连接，请稍候再试'}}
      }
    }
  }
}

export default new Http()
